package datastructure;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author XY
 * @version 1.0
 * @date 2022/1/20 9:11
 * @Description  有效的括号
 */
public class Leet20 {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack();
        char[] chars = s.toCharArray();
        if(chars.length%2!=0){
            return false;
        }
        Map<Character, Character> map =new HashMap<>();
        map.put(')','(');
        map.put(']','[');
        map.put('}','{');
        for (char aChar : chars) {
            if(aChar=='('||aChar=='['||aChar=='{'){
                stack.push(aChar);
            }else{
                if(stack.isEmpty()){
                    return false;
                }
                Character pop = stack.pop();
                if(pop!=map.get(aChar)){
                    return false;
                }

            }
        }
        if(!stack.isEmpty()){
            return false;
        }
        return true;
    }
}
